how do I use key word arguments with python multiprocessing pool apply 您所在的位置:网站首页 python keywords how do I use key word arguments with python multiprocessing pool apply

how do I use key word arguments with python multiprocessing pool apply

2023-03-22 14:05| 来源: 网络整理| 查看: 265

Answer a question

I'm trying to get to grips with pythons multiprocessing module, specifically the apply_async method of Pool. I'm trying to call a function with arguments and keyword arguments. If I call the function without kwargs it's fine but when I try to add in a keyword argument I get: TypeError: apply_async() got an unexpected keyword argument 'arg2' Below is the test code that I'm running

#!/usr/bin/env python import multiprocessing from time import sleep def test(arg1, arg2=1, arg3=2): sleep(5) if __name__ == '__main__': pool = multiprocessing.Pool() for t in range(1000): pool.apply_async(test, t, arg2=5) pool.close() pool.join()

How can I call the function so that it accepts keyword arguments?

Answers

Pass the keyword args in a dictionary (and the positional arguments in a tuple):

pool.apply_async(test, (t,), dict(arg2=5))


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有